package corn

import (
	"errors"
	"gitee.com/yysf-xin/dproxy/pkg/app"
	"gitee.com/yysf-xin/dproxy/pkg/model"
	"github.com/robfig/cron/v3"
)

const (
	ClashTask uint = iota + 1
	AliyunDDNS
	ClashCleanTask
)

type JobCreator func(ctl *Control, cfg *model.CornConfig) (cron.Job, error)

var (
	jobCreatorMap = make(map[uint]JobCreator)
	dict          *app.DictLite
)

func RegJob(t uint, name string, creator JobCreator) {
	dict.Children = append(dict.Children.([]*app.DictLite), &app.DictLite{
		Label: name,
		Value: t,
	})
	jobCreatorMap[t] = creator
}
func GetJob(t uint) (c JobCreator, err error) {
	c = jobCreatorMap[t]
	if c == nil {
		err = errors.New("creator is nil")
	}
	return
}

func init() {
	dict = app.GetDict("corn")
	dict.Children = make([]*app.DictLite, 0)
}
